/**
 * 服务实现样例
 */
package elasticsearch

import (
	"fmt"

	s "gitee.com/xiaoa7/jsetl/service"
	"github.com/elastic/go-elasticsearch/v8"
)

type (
	ElasticsearchService struct {
		client *elasticsearch.Client
	}
)

// 注册服务
func RegisteService(key string, addr string) error {
	//初始化ES连接
	cfg := elasticsearch.Config{
		Addresses: []string{
			addr,
		},
	}
	// 连接到ElasticSearch
	client, err := elasticsearch.NewClient(cfg)
	if err != nil {
		return fmt.Errorf("连接ElasticSearch失败: %v", err)
	}
	// 验证连接
	res, err := client.Info()
	if err != nil {
		return fmt.Errorf("验证ElasticSearch连接失败: %v", err)
	}
	defer res.Body.Close()
	if res.IsError() {
		return fmt.Errorf("ElasticSearch连接错误: %s", res.Status())
	}
	s.RegisteService(key, &ElasticsearchService{client})
	return nil
}

//实现ServcieFace接口

func (d *ElasticsearchService) Connect(key string, p ...string) error {
	return nil
}

func (d *ElasticsearchService) Destory() error {
	return nil
}
